package StudentManagementSystem;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentMain {
    public static void main(String[] args) {
        ArrayList<Student>list=new ArrayList<Student>();
        Scanner sc=new Scanner(System.in);
        Loop: while (true) {
            System.out.println("---------欢迎来到黑马管理系统----------");
            System.out.println("1.填加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出");
            System.out.println("请输入您的选择");
            String choose=sc.next();
            switch(choose){
                case "1"-> addStudent(list);
                case "2"-> deleteStudent(list);
                case "3"-> updateStudent(list);
                case "4"-> quaryStudent(list);
                case "5"-> {
                    System.out.println("退出");
                break Loop;//指定跳出的循环(在嵌套循环中使用)
                    //System.exit(0);虚拟机停止运行：只全部代码停止运行
                }
                default-> System.out.println("没有这个选项");
            }
        }
    }
    public static void addStudent(ArrayList<Student>list){
        Student s=new Student();
        Scanner sc=new Scanner(System.in);
        System.out.println("添加学生");
        System.out.println("请输入学生的姓名");
        String name=sc.next();
        s.setName(name);
        String id=null;
        while (true) {
            System.out.println("请输入学生的学号");
            id = sc.next();
            boolean flag = contains(list, id);
            if (flag) {
                System.out.println("学号已存在，请重新输入");
            } else {
                s.setId(id);
                break;
            }
        }
        System.out.println("请输入学生的年龄");
        int age=sc.nextInt();
        s.setAge(age);
        System.out.println("请输入学生的家庭住址");
        String address=sc.next();
        s.setAddress(address);
        list.add(s);
        System.out.println("恭喜您，数据添加成功");
    }
    public static void deleteStudent(ArrayList<Student>list){
        Scanner sc=new Scanner(System.in);
        String id=sc.next();
        System.out.println("请输入删除学生的id");
        int index= getIdIndex(list,id);
        if (index>=0){
            list.remove(id);
            System.out.println("该学生的id删除成功");
        }else {
            System.out.println("该学生id不存在，删除失败");
        }
    }
    public static void updateStudent(ArrayList<Student>list){
        System.out.println("修改学生");
        Scanner sc=new Scanner(System.in);
        String id=sc.next();
        int index=getIdIndex(list,id);
        if (index==-1){
            System.out.println("需要修改的id："+id+"不存在，请重新输入");
            return;
        }
        Student stu=list.get(index);
        System.out.println("请输入要修改的学生姓名");
        String NewName=sc.next();
        stu.setName(NewName);
        System.out.println("请输入要修改的学生年龄");
        int NewAge=sc.nextInt();
        stu.setAge(NewAge);
        System.out.println("请输入要修改的学生id");
        String NewId=sc.next();
        stu.setId(NewId);
        System.out.println("请输入要修改的学生家庭住址");
        String NewAddress=sc.next();
        stu.setAddress(NewAddress);
        System.out.println("学生信息修改成功");
    }
    public static void quaryStudent(ArrayList<Student>list){
        System.out.println("查询学生");
        if (list.size() == 0) {

            System.out.println("目前没有学生信息，请添加后查询");
            return;
        }
        System.out.println("id\t\t姓名\t年龄\t家庭住址");
        for (int i = 0; i < list.size(); i++) {
            Student stu=list.get(i);
            System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());
        }
    }
    public static boolean contains(ArrayList<Student>list,String id){
        /*for (int i = 0; i < list.size(); i++) {
            Student stu=list.get(i);
            String std=stu.getId();
            if (std.equals(id)){
                return true;
            }
        }*/
        return getIdIndex(list,id)>=0;
    }
    public static int getIdIndex(ArrayList<Student>list,String id){
        //遍历集合
        for (int i = 0; i < list.size(); i++) {
            //得到每一个学生对象
            Student stu=list.get(i);
            //得到每个学生的id
            String std=stu.getId();
            //将得到的id跟删除的id进行比较
            if (std.equals(id)){
                //如果一样，那么返回索引
                return i;
            }
        }
        return  -1;
    }
    }

